package com.zzk.lanouhn.myapplication1.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.InvestPingLunDetailsAdapter;
import com.zzk.lanouhn.myapplication1.modles.InvestPinglunDetailsModle;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/16.
 */
public class InvestPingLunDetails extends AppCompatActivity {
    private ImageView iv_fanhui;

    private XRecyclerView xRecyclerView;

    private InvestPingLunDetailsAdapter adapter;

    private int page = 1;
    private List<InvestPinglunDetailsModle.DataBean> mlist = new ArrayList<>();
    private List<InvestPinglunDetailsModle.DataBean> tempList = new ArrayList<>();

    private SharedPreferences sp;
    private String stringID;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invest_pinglun_details);
        iv_fanhui = (ImageView) findViewById(R.id.invest_pinglun_iv_fanhui);
        xRecyclerView = (XRecyclerView) findViewById(R.id.invest_pinglun_xrecyclerView);

        sp = getSharedPreferences("InvestID", MODE_PRIVATE);
        stringID = sp.getString("iID", "12345");

        LinearLayoutManager layoutManager = new LinearLayoutManager(InvestPingLunDetails.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);

        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        xRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 1;
                getDataInvestOne();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                getDataInvestOne();
            }
        });

        getDataInvestOne();
        inivData();
    }

    private void inivData() {
        iv_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getDataInvestOne() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String ssPage = Integer.toString(page);
                String url = "http://lol.zhangyoubao.com/apis/rest/ItemsService/commentsById?" +
                        "id=" + stringID + "&page=" + ssPage + "&userid=&token=&i_=K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=" +
                        "&deviceId=K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&t_=1474208683106&p_=" +
                        "11663&v_=400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=" +
                        "22&cha=baiduMartket&u_=&modle_=vivo%20X7&api=ItemsService.commentsById&time" +
                        "=1474208683&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion" +
                        "=v1.0&nonce=620718&secretSignature=a%2Bd8wWGIft0P559PNUbwF8d9gBk= HTTP/1.1";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String ss = response.body().string();
                        Gson gson = new Gson();

                        tempList = gson.fromJson(ss, InvestPinglunDetailsModle.class).getData();
                        if (tempList.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }

                    }
                });


            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(InvestPingLunDetails.this, "失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 1) {
                        mlist.clear();
                        mlist.addAll(tempList);

                        adapter = new InvestPingLunDetailsAdapter(InvestPingLunDetails.this, mlist);
                        xRecyclerView.setAdapter(adapter);

                        xRecyclerView.refreshComplete();
                        tempList.clear();
                    } else {
                        mlist.addAll(tempList);
                        adapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                        tempList.clear();
                    }
                    break;
                case 2:
                    Toast.makeText(InvestPingLunDetails.this, "还是失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


}
